package first;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;

public class Classroom implements Iterable<Student> {

	private int num;
	private Set<Student> students;
	
	public Classroom(int num) {
		this.num = num;
		students = new HashSet<Student>();
	}
	
	public void fillRoom() {
		for (int i = 0; i < this.num; i++) {
			students.add(new Student(i+1));
		}
		for (Student stu: students) {
			stu.addObserver(new ConsolWriter());
		}
	}
	
	public void giveName() {
		for (Student stu: this) {
			stu.name(generateStr(5));
		}
	}
	
	// Random string generator
	private String generateStr(int len) {
		char[] chars = {'a','e','i','o','u','m','n','l','s','p'}; // Set of chars used to generate string
		char[] result = new char[len];
		int randnum;
		Random random = new Random();
		for (int i = 0; i < len; i++) {
			randnum = random.nextInt(chars.length);
			result[i] = chars[randnum];
		}
		return String.copyValueOf(result);
	}

	@Override
	public Iterator<Student> iterator() {
		return students.iterator();
	}
}
